/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.security.applicationListner;

import cc.altius.domain.CustomUserDetails;
import cc.altius.service.UserService;
import cc.altius.utils.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

/**
 *
 * @author Akil Mahimwala
 */
public class SuccessEvent implements ApplicationListener<AuthenticationSuccessEvent> {

    @Autowired
    private UserService userService;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent e) {
        LogUtils.debugLogger.debug("Inside the success event");
        CustomUserDetails curUser = (CustomUserDetails) e.getAuthentication().getPrincipal();
        this.userService.resetFailedCountForUserId(curUser.getUserId());
        LogUtils.loginSuccess(((WebAuthenticationDetails)e.getAuthentication().getDetails()).getRemoteAddress(), curUser.getUserId(), curUser.getUsername());
    }

}
